package com.webchat.admin.controller;

import com.webchat.admin.service.PaymentAppService;
import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.config.annotation.ValidatePermission;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.vo.request.payment.CreateAppRequestVO;
import com.webchat.domain.vo.response.payment.AppBaseResponseVO;
import com.webchat.domain.vo.response.payment.CreateAppResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
@RequestMapping("/admin-service/payment/app")
public class PaymentAppController {


    @Autowired
    private PaymentAppService paymentAppService;

    /**
     * 支付平台创建应用（接入方）
     *
     * @param createAppRequest
     * @return
     */
    @ValidatePermission
    @PostMapping("/create")
    public APIResponseBean<CreateAppResponseVO> createApp(@RequestBody CreateAppRequestVO createAppRequest) {
        String userId = SessionHelper.getCurrentUserId();
        createAppRequest.setUserId(userId);
        CreateAppResponseVO app = paymentAppService.createApp(createAppRequest);
        return APIResponseBeanUtil.success(app);
    }

    /**
     * 发布app
     *
     * @return
     */
    @ValidatePermission
    @PostMapping("/publish/{appId}")
    public APIResponseBean<Boolean> publishApp(@PathVariable Long appId) {
        boolean result = paymentAppService.publishApp(appId);
        String userId = SessionHelper.getCurrentUserId();
        log.info("{}发布应用{},操作结果:{}", userId, appId, result);
        return APIResponseBeanUtil.success(result);
    }

    /**
     * 删除app
     *
     * @param appId
     * @return
     */
    @ValidatePermission
    @PostMapping("/delete/{appId}")
    public APIResponseBean<Boolean> deleteApp(@PathVariable Long appId) {
        boolean result = paymentAppService.deleteApp(appId);
        String userId = SessionHelper.getCurrentUserId();
        log.info("{}删除应用{},操作结果:{}", userId, appId, result);
        return APIResponseBeanUtil.success(result);
    }


    /**
     * 应用列表
     *
     * @param keywords
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ValidatePermission
    @GetMapping("/page")
    public APIPageResponseBean<AppBaseResponseVO> page(@RequestParam(value = "keywords", required = false) String keywords,
                                                @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        return paymentAppService.page(keywords, pageNo, pageSize);
    }
}
